package com.yunsc.shop.controller.user;

import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.shop.utils.ClientInfoUtils;
import com.yunsc.store.dto.UserAddressDTO;
import com.yunsc.store.entity.YunscUserAddressEntity;
import com.yunsc.store.service.YunscUserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 用户地址管理
 * @date: 2024-05-11 23:13
 */
@Api(tags = "用户地址管理")
@RestController("userCategoryController")
@RequestMapping("address")
public class AddressController {

    @Resource
    private YunscUserAddressService userAddressService;
    @Resource
    private ClientInfoUtils clientInfoUtils;

    @ApiOperation("查询用户地址")
    @GetMapping("/list")
    public Result<?> queryUserAddress() {
        Integer userId = SecurityUtil.getCurrentUserId();
        List<YunscUserAddressEntity> list = userAddressService.queryUserAddressList(userId);
        return Result.resultList(list);
    }

    // /address/defaultId
    @ApiOperation("查询用户默认地址")
    @GetMapping("/defaultId")
    public Result<?> setDefaultAddress() {
        Integer userId = SecurityUtil.getCurrentUserId();
        int defaultId = userAddressService.queryUserDefaultAddress(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("defaultId", defaultId);
        return Result.ok(map);
    }

    @ApiOperation("设置用户默认地址")
    @PostMapping("/setDefault")
    public Result<?> queryUserDefaultAddress(@RequestBody() @Validated({ValidationGroups.Update.class}) UserAddressDTO params) {
        Integer userId = SecurityUtil.getCurrentUserId();
        userAddressService.setUserDefaultAddress(Integer.valueOf(params.getAddressId()), userId);
        return Result.ok("设置成功");
    }

    @ApiOperation("新增用户地址")
    @PostMapping("/add")
    public Result<?>  addUserAddress(@RequestBody @Validated({ValidationGroups.Insert.class}) UserAddressDTO params) {
        Integer userId = SecurityUtil.getCurrentUserId();
        Integer currentStoreId = clientInfoUtils.getCurrentStoreId();
        userAddressService.addUserAddress(params.getForm(), userId, currentStoreId);
        return Result.ok("添加成功");
    }

    @ApiOperation("查询用户地址详情")
    @GetMapping("/detail")
    public Result<?> queryUserDetailAddress(@RequestParam("addressId") Integer addressId) {
        YunscUserAddressEntity address = userAddressService.queryUserAddressDetail(addressId);
        Map<String, Object> map = new HashMap<>();
        map.put("detail", address);
        return Result.ok(map);
    }

    @ApiOperation("编辑用户地址")
    @PostMapping("/edit")
    public Result<?> editUserAddress(@RequestBody @Validated({ValidationGroups.Update.class}) UserAddressDTO params) {
        Integer userId = SecurityUtil.getCurrentUserId();
        userAddressService.editUserAddress(userId, params);
        return Result.ok("编辑成功");
    }

    @ApiOperation("编辑用户地址")
    @PostMapping("/remove")
    public Result<?> removeUserAddress(@RequestBody @Validated({ValidationGroups.Delete.class}) UserAddressDTO addressForm) {
        Integer userId = SecurityUtil.getCurrentUserId();
        userAddressService.removeUserAddress(addressForm, userId);
        return Result.ok("删除成功");
    }






}
